package yxy.game.pm2.command.camptower;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.battle.BattleHandle;

/**
 * 阵营塔阵容改变接口
 *
 * @ClassName CMD_CampTowerChangeLineup
 * @Author jingtao-zheng
 * @Date 2022/8/1 9:47
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerChangeLineup, classtype = CampTowerMessage.CampTowerChangeLineupRequest.class)
public class CMD_CampTowerChangeLineup extends Command<CampTowerMessage.CampTowerChangeLineupRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerChangeLineupRequest request) throws Exception {
        try {
            CampTowerMessage.CampTowerChangeLineupResponse.Builder response = CampTowerMessage.CampTowerChangeLineupResponse.newBuilder();
            String playerCode = messageSource.getTag();
            BattleHandle battleHandle = new BattleHandle();

            //获取英雄数组
            String herosCodeJsonArrary = request.getHerosCodeJsonArrary();
            JSONArray jsonArray = JSONArray.parseArray(herosCodeJsonArrary);

            //判断阵容需不需要重新计算羁绊
            int nb = request.getNB();
            Format format = getFormat(jsonArray, nb);

            //封装返回  [{'code':'','power':''},{'code':'','power':''} ]
            JSONArray returnJSONArrary = getReturn(jsonArray, playerCode, format, battleHandle);

            response.setHerosJsonArrary(returnJSONArrary.toString());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerChangeLineupResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    //封装阵容
    private Format getFormat(JSONArray jsonArray, int nb) {
        Format format = new Format();
        for (int i = 0; i < jsonArray.size(); i++) {
            if (StringUtils.isEmpty(format.getFu())) {
                format.setFu(jsonArray.getString(i));
                continue;
            }
            if (StringUtils.isEmpty(format.getFm())) {
                format.setFm(jsonArray.getString(i));
                continue;
            }
            if (StringUtils.isEmpty(format.getFd())) {
                format.setFd(jsonArray.getString(i));
                continue;
            }
            if (StringUtils.isEmpty(format.getBu())) {
                format.setBu(jsonArray.getString(i));
                continue;
            }
            if (StringUtils.isEmpty(format.getBm())) {
                format.setBm(jsonArray.getString(i));
                continue;
            }
            if (StringUtils.isEmpty(format.getBd())) {
                format.setBd(jsonArray.getString(i));
                continue;
            }
        }

        if (nb > 0) format.setNb(nb);

        return format;
    }

    //封装返回
    private JSONArray getReturn(JSONArray jsonArray, String playerCode, Format format, BattleHandle battleHandle) {
        JSONArray returnJSONArrary = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            String code = jsonArray.getString(i);
            Hero hero = Hero.get(playerCode, code);
            long power = battleHandle.calculatePower(playerCode, hero, format);
            jsonObject.put(code, power);
            returnJSONArrary.add(jsonObject);
        }
        return returnJSONArrary;
    }
}
